package x.zero.controller.sys;



import x.zero.annotation.SysLog;
import x.zero.pojo.entity.SysSchool;
import x.zero.service.sys.SysSchoolService;
import x.zero.util.CommonResult;
import x.zero.util.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

/**
 * <p>
 * <p>
 * </p>
 */
@Slf4j
@RestController
@Api(tags = "SysSchoolController", description = "管理")
@RequestMapping("/sys/SysSchool")
public class SysSchoolController {
    @Autowired
    private SysSchoolService SysSchoolService;

    @SysLog(MODULE = "sys", REMARK = "根据条件查询所有列表")
    @ApiOperation("根据条件查询所有列表")
    @GetMapping(value = "/list")
    @PreAuthorize("hasAuthority('sys:SysSchool:read')")
    public Object getRoleByPage(SysSchool entity,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        try {
            return CommonResult.success(SysSchoolService.page(pageNum,pageSize,entity));
        } catch (Exception e) {
            log.error("根据条件查询所有列表：%s", e.getMessage(), e);
        }
        return CommonResult.failed();
    }

    @SysLog(MODULE = "sys", REMARK = "保存")
    @ApiOperation("保存")
    @PostMapping(value = "/create")
    @PreAuthorize("hasAuthority('sys:SysSchool:create')")
    public Object saveRole(@RequestBody SysSchool entity) {
        try {
            if (SysSchoolService.save(entity)) {
                return CommonResult.success();
            }
        } catch (Exception e) {
            log.error("保存：%s", e.getMessage(), e);
            return CommonResult.failed();
        }
        return CommonResult.failed();
    }

    @SysLog(MODULE = "sys", REMARK = "更新")
    @ApiOperation("更新")
    @PostMapping(value = "/update/{ids}")
    @PreAuthorize("hasAuthority('sys:SysSchool:update')")
    public Object updateRole(@RequestBody SysSchool entity) {
        try {
            if (SysSchoolService.updateById(entity)) {
                return CommonResult.success();
            }
        } catch (Exception e) {
            log.error("更新：%s", e.getMessage(), e);
            return CommonResult.failed();
        }
        return CommonResult.failed();
    }

    @SysLog(MODULE = "sys", REMARK = "删除")
    @ApiOperation("删除")
    @GetMapping(value = "/delete/{ids}")
    @PreAuthorize("hasAuthority('sys:SysSchool:delete')")
    public Object deleteRole(@ApiParam("ids") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return CommonResult.paramFailed("id");
            }
            if (SysSchoolService.deleteById(id)) {
                return CommonResult.success();
            }
        } catch (Exception e) {
            log.error("删除：%s", e.getMessage(), e);
            return CommonResult.failed();
        }
        return CommonResult.failed();
    }

    @SysLog(MODULE = "sys", REMARK = "给分配")
    @ApiOperation("查询明细")
    @GetMapping(value = "/{id}")
    @PreAuthorize("hasAuthority('sys:SysSchool:read')")
    public Object getRoleById(@ApiParam("ids") @PathVariable Long id) {
        try {
            if (ValidatorUtils.empty(id)) {
                return CommonResult.paramFailed("id");
            }
            SysSchool coupon = SysSchoolService.queryById(id);
            return CommonResult.success(coupon);
        } catch (Exception e) {
            log.error("查询明细：%s", e.getMessage(), e);
            return CommonResult.failed();
        }

    }

    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/delete/batch", method = RequestMethod.GET)
    @ResponseBody
    @SysLog(MODULE = "pms", REMARK = "批量删除")
    @PreAuthorize("hasAuthority('sys:SysSchool:delete')")
    public Object deleteBatch(@RequestParam("ids") List<Long> ids) {
        boolean count = SysSchoolService.deleteById(ids);
        if (count) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

}
